package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.DataExt;
import com.symone.webcms.model.base.ResultMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.service.DataExtService;
import com.symone.webcms.model.DataExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *   @description : ContentExt 管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-03-24
 */
@RestController
@Api(value="/contentExt", description="ContentExt 管理")
@RequestMapping("/contentExt")
public class DataExtController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(DataExtController.class);

    @Autowired
    public DataExtService dataExtService;

    /**
     * @description : 获取ContentExt分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-24
     */
    @RequiresPermissions("contentExt:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ApiOperation(value="获取ContentExt分页列表", notes="获取ContentExt分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageSize",value="页大小",required=true,defaultValue = "10"),
            @ApiImplicitParam(name="pageNum",value="页数",required=true,defaultValue = "1")
    })
    public ResultMessage page(Integer pageSize,Integer pageNum) {
        Wrapper<DataExt> ew = new EntityWrapper<DataExt>();
        ew.where("deleted=0");

        //logger.debug("SQL:"+ew.originalSql());

        Page<DataExt> page = new Page<DataExt>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        page = dataExtService.findPage(page, ew);

        return ResultMessage.success("获取ContentExt的分页列表",page.getRecords(),page.getTotal());
    }

    /**
     * @description : 获取所有ContentExt记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-24
     */
    @RequiresPermissions("contentExt:index")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value="获取所有ContentExt信息", notes="获取所有ContentExt信息")
    public ResultMessage list() {
        return ResultMessage.success("获取所有ContentExt信息", dataExtService.findAll());
    }

    /**
     * @description : 通过id获取ContentExt
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-24
     */
    @RequiresPermissions("contentExt:get")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ApiOperation(value="获取ContentExt信息", notes="获取ContentExt信息")
    @ApiImplicitParam(name="id",value="ContentExtID",required=true)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取ContentExt信息成功!", dataExtService.selectById(id));
    }

    /**
     * @description : 添加ContentExt
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-24
     */
    @RequiresPermissions("contentExt:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value="添加ContentExt信息", notes="添加ContentExt信息")
    @ApiImplicitParam(name = "contentExt", value = "ContentExt信息", required = true )
    public ResultMessage add(DataExt dataExt){
        dataExtService.insert(dataExt);
        return ResultMessage.success("添加ContentExt成功!");
    }

    /**
     * @description : 通过id逻辑删除ContentExt
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-24
     */
    @RequiresPermissions("contentExt:remove_flag")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    @ApiOperation(value="逻辑删除ContentExt", notes="逻辑删除ContentExt")
    @ApiImplicitParam(name = "id", value = "ContentExtId", required = true )
    public ResultMessage removeFlag(Long id){
        if(dataExtService.deleteFlagById(id)) {
            return ResultMessage.success("逻辑ContentExt删除成功！");
        } else {
            return ResultMessage.fail("逻辑ContentExt删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除ContentExt
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-24
     */
    @RequiresPermissions("contentExt:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ApiOperation(value="物理删除ContentExt", notes="物理删除ContentExt")
    @ApiImplicitParam(name = "id", value = "ContentExtId", required = true )
    public ResultMessage remove(long id){
        if(dataExtService.deleteById(id)) {
            return ResultMessage.success("物理删除ContentExt成功！");
        } else {
            return ResultMessage.fail("物理删除ContentExt失败！");
        }
    }

    /**
     * @description : 更新ContentExt信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-24
     */
    @RequiresPermissions("contentExt:edit")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ApiOperation(value="更新ContentExt信息", notes="更新ContentExt信息")
    @ApiImplicitParam(name = "contentExt", value = "ContentExt信息", required = true )
    public ResultMessage update(DataExt dataExt) {
        if(dataExtService.updateById(dataExt)) {
            return ResultMessage.success("ContentExt信息更新成功!");
        }else{
            return ResultMessage.fail("ContentExt信息更新失败!");
        }
    }

    /**
    * @description : 清除ContentExt记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-24
    */
    @RequiresPermissions("contentExt:clear")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    @ApiOperation(value="清除ContentExt记录", notes="清除已被逻辑删除的ContentExt记录")
    public ResultMessage clear(){
        int num = dataExtService.clear();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有ContentExt记录被清除！");
        }
    }



}
